package com.ruoyi.shop.nosql.mongo.service.impl;

import com.ruoyi.shop.common.constant.Constants;
import com.ruoyi.shop.common.utils.BeanConvertUtils;
import com.ruoyi.shop.nosql.mongo.domain.Message;
import com.ruoyi.shop.nosql.mongo.service.IMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @Author: JinChen Zhu
 * @CreateTime: 2024-07-01  01:07
 */
@Service
public class MessageServiceImpl implements IMessageService {

    @Autowired
    private MongoTemplate mongoTemplate;


//    // 创建和更新
//    public Message save(Message chatMessage) {
//        return chatMessageRepository.save(chatMessage);
//    }
//
//    // 查询所有
//    public List<Message> findAll() {
//        return chatMessageRepository.findAll();
//    }
//
//    // 根据ID查询
//    public Optional<Message> findById(String id) {
//        return chatMessageRepository.findById(id);
//    }
//
//    // 删除
//    public void deleteById(String id) {
//        chatMessageRepository.deleteById(id);
//    }

    //根据用户id查询
//    public List<Message> findByUserId(String userId) {
//        chatMessageRepository.f;
//    }


    @Override
    public void addMessage(Message message) {
        //mongoDB 保存消息
        mongoTemplate.save(message, Constants.NOTICE_COLLECTION);
    }

    /**
     * 根据用户ID 获取消息
     *
     * @param receiverIds
     * @return
     */
    @Override
    public List<Message> getNoticeByUserIds(List<String> receiverIds) {
        //根据用户ID获取消息 并获取前十条
        Criteria criteria = Criteria.where("receiverId").in(receiverIds);
        criteria.andOperator(Criteria.where("isRead").is(false));
        Query query = new Query(criteria);

        Update update = Update.update("isRead", true);
        // 查询消息并返回结果列表
        List<Message> messageList = mongoTemplate.find(query, Message.class, Constants.NOTICE_COLLECTION);

        // 如果结果列表不为空，则更新已读状态为true
        if (!messageList.isEmpty()) {
            mongoTemplate.updateMulti(query, update, Message.class, Constants.NOTICE_COLLECTION);
        }

        // 返回消息列表
        return messageList;
    }


    @Override
    public List<Message> queryList(Message message) {
        // 创建Criteria对象
        Criteria criteria = new Criteria();
        List<Criteria> orCriteriaList = new ArrayList<>();

        orCriteriaList.add(Criteria.where("receiverId").in(message.getReceiverId()).andOperator(Criteria.where("senderId").in(message.getSenderId())));
        orCriteriaList.add(Criteria.where("senderId").in(message.getReceiverId()).andOperator(Criteria.where("receiverId").in(message.getSenderId())));

        criteria.orOperator(orCriteriaList.toArray(new Criteria[0]));

        Query query = new Query(criteria);
        query.limit(20);
        // 直接设置为升序排列，避免后续反转列表
        query.with(Sort.by(Sort.Order.asc("time")));

        List<Message> messageList = mongoTemplate.find(query, Message.class, Constants.NOTICE_COLLECTION);
        return messageList;
    }



}

